﻿using System;
using System.Collections.Generic;
using System.Linq;
//using System.Net;
using System.Device.Location;
using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Animation;
using System.Windows.Navigation;
//using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

using BEO.BingSearchService;
using Microsoft.Phone.Controls.Maps;

namespace BEO
{
    public partial class View : PhoneApplicationPage
    {
        private GeoCoordinate mGeoCoordinate;
        private Errand mErrand;
        public View()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            
            string id = null;
            if (NavigationContext.QueryString.TryGetValue("id", out id))
            {
                // get the errand
                mErrand = (from _errand in (App.Current as App).Errands 
                                 where _errand.Id.ToString().Equals(id) 
                                 select _errand
                                 ).FirstOrDefault();

                // consume the picture key/val pair...
                NavigationContext.QueryString.Remove("id");

                // update page information
                mGeoCoordinate = new GeoCoordinate(mErrand.Latitude, mErrand.Longitude);

                // set pushpin
                Pushpin mPin = new Pushpin();
                mPin.Location = mGeoCoordinate;
                mMLMap.Children.Add(mPin);

                // center silverlight map control
                mMLMap.Center = mGeoCoordinate;

                //Ugly but will work, sets the default zoom in level
                GeoCoordinate[] mGeoCoords = new GeoCoordinate[2];
                mGeoCoords[0] = mMLMap.Center;
                GeoCoordinate rectOffsetCoord = new GeoCoordinate(mMLMap.Center.Latitude, mMLMap.Center.Longitude);
                rectOffsetCoord.Longitude -= .001f;
                rectOffsetCoord.Longitude -= .001f;
                mGeoCoords[1] = rectOffsetCoord;
                mMLMap.SetView(LocationRect.CreateLocationRect(mGeoCoords));

                // set information
                tbAddress.Text = mErrand.ToString();
            }

            base.OnNavigatedTo(e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string caption = "Delete Errand?";
            string text = "Click ok to permanently delete the errand, otherwise click cancel.";
            MessageBoxResult mbr = MessageBox.Show(text, caption, MessageBoxButton.OKCancel);

            if (mbr.Equals(MessageBoxResult.OK))
            {
                (App.Current as App).Errands.Remove(mErrand);
                NavigationService.GoBack();
            }
        }

        private void mMLMap_MapZoom(object sender, MapZoomEventArgs e)
        {
            e.Handled = true;
        }

        private void RecenterButtonImage_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            mMLMap.Center = mGeoCoordinate;
            e.Handled = true;
        }
    }
}